package DT3;

import java.util.Date;
import javagprssrv.Common.DateTime;
import javagprssrv.Common.CRC;

public class DT3_Util
{
    java.lang.String TermNO;
    
    public DT3_Util() {
        this.TermNO = "";
    }
    
    public java.lang.String MakeCommand(final java.lang.String _TermNO, java.lang.String _cmd) {
        java.lang.String res = "";
        _cmd += "                              ";
        this.TermNO = _TermNO;
        if (!_cmd.substring(0, 9).equals("GET TIME ")) {
            res = this.MakeCommand_getTime(_cmd);
        }
        if (!_cmd.substring(0, 9).equals("SET TIME ")) {
            res = this.MakeCommand_setTime(_cmd);
        }
        if (!_cmd.substring(0, 10).equals("GET PARAM ")) {
            res = this.MakeCommand_getParam(_cmd);
        }
        if (!_cmd.substring(0, 10).equals("SET PARAM ")) {
            res = this.MakeCommand_setParam(_cmd);
        }
        if (!_cmd.substring(0, 9).equals("GET DATE ")) {
            res = this.MakeCommand_getDate(_cmd);
        }
        if (!_cmd.substring(0, 9).equals("SET DATE ")) {
            res = this.MakeCommand_setDate(_cmd);
        }
        if (!_cmd.substring(0, 10).equals("GET MONTH ")) {
            res = this.MakeCommand_getMonth(_cmd);
        }
        if (!_cmd.substring(0, 10).equals("SET MONTH ")) {
            res = this.MakeCommand_setMonth(_cmd);
        }
        return res;
    }
    
    private java.lang.String MakeCommand_getTime(java.lang.String job) {
        java.lang.String res = "";
        job = job.toUpperCase();
        if (!job.substring(0, 9).equals("GET TIME ")) {
            return "";
        }
        job = job.trim();
        java.lang.String cmdString = "A" + this.TermNO;
        cmdString = cmdString + '\r' + "0000Z";
        cmdString = (res = cmdString + javagprssrv.Common.CRC.CRC16(cmdString));
        return res;
    }
    
    private java.lang.String MakeCommand_setTime(java.lang.String job) {
        java.lang.String res = "";
        job = job.toUpperCase();
        if (!job.substring(0, 9).equals("SET TIME ")) {
            return "";
        }
        job = job.trim();
        java.lang.String cmdString = "A" + this.TermNO;
        if (job.substring(9, 21).toUpperCase().equals("$SERVERTIME$")) {
            cmdString = cmdString + '-' + "0000" + new javagprssrv.Common.DateTime().getDateTime("YYYYMMDDHHNNSS").substring(2) + "Z";
        }
        else {
            cmdString = cmdString + '-' + "0000" + job.substring(9, 21) + "Z";
        }
        cmdString = (res = cmdString + javagprssrv.Common.CRC.CRC16(cmdString));
        return res;
    }
    
    private java.lang.String MakeCommand_getParam(java.lang.String job) {
        java.lang.String res = "";
        job = job.toUpperCase();
        if (!job.substring(0, 10).equals("GET PARAM ")) {
            return "";
        }
        job = job.trim();
        java.lang.String cmdString = "A" + this.TermNO;
        cmdString = cmdString + '\u000e' + "0000Z";
        cmdString = (res = cmdString + javagprssrv.Common.CRC.CRC16(cmdString));
        return res;
    }
    
    private java.lang.String MakeCommand_setParam(java.lang.String job) {
        java.lang.String res = "";
        job = job.toUpperCase();
        if (!job.substring(0, 10).equals("SET PARAM ")) {
            return "";
        }
        job = job.trim();
        java.lang.String cmdString = "A" + this.TermNO;
        cmdString = cmdString + '.' + "0000" + job.trim().substring(10) + "Z";
        cmdString = (res = cmdString + javagprssrv.Common.CRC.CRC16(cmdString));
        return res;
    }
    
    private java.lang.String MakeCommand_getDate(java.lang.String job) {
        java.lang.String res = "";
        job = job.toUpperCase();
        if (!job.substring(0, 9).equals("GET DATE ")) {
            return "";
        }
        job = job.trim();
        java.lang.String cmdString = "A" + this.TermNO;
        final int month = java.lang.Integer.valueOf(job.substring(9, 11));
        final int date = java.lang.Integer.valueOf(job.substring(11, 13));
        final java.util.Date dt = new java.util.Date();
        final int currMonth = dt.getMonth() + 1;
        java.lang.String cmd = "";
        if (month == currMonth) {
            cmd = java.lang.String.valueOf('\u0004');
        }
        else {
            cmd = java.lang.String.valueOf('\u0005');
        }
        cmdString += cmd;
        if (date >= 10) {
            cmdString += java.lang.String.valueOf(date);
        }
        else {
            cmdString = cmdString + "0" + java.lang.String.valueOf(date);
        }
        cmdString += "Z";
        cmdString = (res = cmdString + javagprssrv.Common.CRC.CRC16(cmdString));
        return res;
    }
    
    private java.lang.String MakeCommand_setDate(java.lang.String job) {
        java.lang.String res = "";
        job = job.toUpperCase();
        if (!job.substring(0, 9).equals("SET DATE ")) {
            return "";
        }
        job = job.trim();
        java.lang.String cmdString = "A" + this.TermNO;
        final int month = java.lang.Integer.valueOf(job.substring(9, 11));
        final int date = java.lang.Integer.valueOf(job.substring(11, 13));
        final java.util.Date dt = new java.util.Date();
        final int currMonth = dt.getMonth() + 1;
        java.lang.String cmd = "";
        if (month == currMonth) {
            cmd = java.lang.String.valueOf('$');
        }
        else {
            cmd = java.lang.String.valueOf('%');
        }
        cmdString += cmd;
        if (date >= 10) {
            cmdString += java.lang.String.valueOf(date);
        }
        else {
            cmdString = cmdString + "0" + java.lang.String.valueOf(date);
        }
        cmdString = cmdString + "00" + new javagprssrv.Common.DateTime().getDateTime("YYYYMMDDHHNNSS").substring(2, 4) + job.substring(9) + "Z";
        cmdString = (res = cmdString + javagprssrv.Common.CRC.CRC16(cmdString));
        return res;
    }
    
    private java.lang.String MakeCommand_getMonth(java.lang.String job) {
        java.lang.String res = "";
        job = job.toUpperCase();
        if (!job.substring(0, 10).equals("GET MONTH ")) {
            return "";
        }
        job = job.trim();
        java.lang.String cmdString = "A" + this.TermNO;
        final java.lang.String cmd = java.lang.String.valueOf('\u0003');
        cmdString += cmd;
        final int month = java.lang.Integer.valueOf(job.substring(10, 12));
        if (month >= 10) {
            cmdString += java.lang.String.valueOf(month);
        }
        else {
            cmdString = cmdString + "0" + java.lang.String.valueOf(month);
        }
        cmdString += "Z";
        cmdString = (res = cmdString + javagprssrv.Common.CRC.CRC16(cmdString));
        return res;
    }
    
    private java.lang.String MakeCommand_setMonth(java.lang.String job) {
        java.lang.String res = "";
        job = job.toUpperCase();
        if (!job.substring(0, 10).equals("SET MONTH ")) {
            return "";
        }
        job = job.trim();
        java.lang.String cmdString = "A" + this.TermNO;
        cmdString += '#';
        cmdString = cmdString + job.substring(10, 12) + "00";
        cmdString += new javagprssrv.Common.DateTime().getDateTime("YYYYMMDD").substring(2, 4);
        cmdString = cmdString + job.substring(10, 12) + "01";
        cmdString += job.substring(12);
        java.lang.String s = "";
        for (int i = 0; i < 96; ++i) {
            s += "0";
        }
        cmdString = cmdString + s + "Z";
        cmdString = (res = cmdString + javagprssrv.Common.CRC.CRC16(cmdString));
        return res;
    }
}
